package a4;
import java.io.*;
//import java.nio.charset.Charset;
//import java.nio.file.FileSystems;
//import java.nio.file.Files;
//import java.nio.file.Path;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;

public class Assignment4 {	
	public static boolean isPos(String s){
		//String temp = s.toLowerCase();
		if('A' <= s.charAt(0) && s.charAt(0) <= 'Z'){
			return true;
		}else{
			return false;
		}
	}

	public static void main(String[] args) {
		ArrayList<String> words = new ArrayList<String>();
		ArrayList<String> pos = new ArrayList<String>();
		ArrayList<String> ppos = new ArrayList<String>();
//		Charset charset = Charset.forName("US-ASCII");
		try {
			File file = new File("CoNLL2009-ST-English-train-pos.txt");
			LineIterator it = FileUtils.lineIterator(file, "US-ASCII");
//			BufferedReader reader = new BufferedReader(new FileReader("CoNLL2009-ST-English-train-pos.txt"));
		    String line = null;
		    while (it.hasNext()) {
		    	line = it.nextLine();
		    	if(line.equals("")) continue;
		        String[] cols = line.split("\t");
		        //if(!isPos(cols[4])) continue;
		        words.add(cols[2]);
		        pos.add(cols[4]);
		    	
		    }
		} catch (IOException x) {
		    System.err.format("IOException: %s%n", x);
		}
		
		CorpusProcesser cp = new CorpusProcesser();
		cp.process(words, pos);
		BaselineTagger bt = new BaselineTagger(cp.getWordCounts());
		
		try {
			File file = new File("CoNLL2009-ST-English-development-pos.txt");
			LineIterator it = FileUtils.lineIterator(file, "US-ASCII");
		    String line = null;
		    while (it.hasNext()) {
		    	line = it.nextLine();
		    	if(line.equals("")) continue;
		        String[] cols = line.split("\t");
		        //if(!isPos(cols[4])) continue;
		        words.add(cols[2]);
		        pos.add(cols[4]);
		    }
		} catch (IOException x) {
		    System.err.format("IOException: %s%n", x);
		}
		
		ppos = bt.tag(words);
		
		Eval pe = new Eval(pos, ppos);
		
		System.out.println(pe.pposAccuracy());
	}
}
